Cousins in a Binary Tree
Question
Given the root of a binary tree and two of its nodes' values, return whether the nodes are cousins of each other.
Nodes are cousins if they have the same depth and different parents.
The values of the nodes in the tree are all unique.
Input: root = [1, 2, 3, 4, 5, None, 7], a = 4, b = 7
Output: True
4 and 6 are cousins because they're at the same depth and have different parents.
Input: root = [1, 2, 3, None, 5], a = 2, b = 3
Output: False
2 and 3 are not cousins because they have the same parents.
Input: root = [1, 2, 3, 4], a = 3, b = 4
Output: False
3 and 4 are not cousins because they have different depths.
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
Login or signup to save your code.
Uh oh... looks like you don't yet have access.
Not sure what this unlocks? Check out a free pattern section.